// These are meant to be typed into the REPL. You can also run
// scala -Xnojline < 02_char.sc to run them all at once.

import scala.io._

val path = Thread.currentThread().getContextClassLoader.getResource("mary.txt").getPath

//val source = Source.fromFile(path, "UTF-8")
//var upper = 0
//for (c <- source) if (c.isUpper) upper += 1
//println(f"$upper uppercase")

val source2 = Source.fromFile(path, "UTF-8")
// 返回一个个字符
val iter = source2.buffered

while (iter.hasNext) {
  // 1）只打印大写字母开头的单词
//  if (iter.head.isUpper) {
//    while (iter.hasNext && !iter.head.isWhitespace)
//      print(iter.next)
//    println()
//  }
//  else iter.next
  // 2）打印每一个字符
//  println(iter.next)
  // 3）打印为大写字母开头的一句话
  if (iter.head.isUpper) {
    var nextCharIsUpper = true
    while (iter.hasNext && nextCharIsUpper) {
      val nextChar = iter.next
      if(!nextChar.isControl) {
        print(nextChar)
      } else {
        print("[\\n]")
      }
      if(iter.hasNext && iter.head.isUpper) {
        nextCharIsUpper = false
      }
    }
    println()
  }
}
source2.close()

//val source3 = Source.fromFile(path, "UTF-8")
//val contents = source3.mkString
//println(f"${contents.substring(0, 10)}...")

